<template>
  <div class="dashboard">
    <header class="dashboard-header">
      <div class="header-left">
        <h1>餐饮管理系统</h1>
      </div>
      <div class="header-right">
        <div class="user-info">
          <span class="welcome">欢迎，{{ userInfo.username }}</span>
          <span class="user-type">{{ userTypeText }}</span>
          <button class="logout-btn" @click="handleLogout">退出登录</button>
        </div>
      </div>
    </header>
    
    <main class="dashboard-main">
      <div class="dashboard-content">
        <div class="welcome-card">
          <h2>欢迎使用餐饮管理系统</h2>
          <p>您已成功登录系统，可以开始使用各项功能。</p>
          
          <div class="user-details">
            <div class="detail-item">
              <label>用户ID:</label>
              <span>{{ userInfo.id }}</span>
            </div>
            <div class="detail-item">
              <label>用户名:</label>
              <span>{{ userInfo.username }}</span>
            </div>
            <div class="detail-item">
              <label>用户类型:</label>
              <span>{{ userTypeText }}</span>
            </div>
            <div class="detail-item" v-if="userInfo.nickname">
              <label>昵称:</label>
              <span>{{ userInfo.nickname }}</span>
            </div>
          </div>
          
          <div class="quick-actions">
            <h3>快速操作</h3>
            <div class="action-grid">
              <div class="action-card">
                <div class="action-icon">📊</div>
                <div class="action-title">数据报表</div>
                <div class="action-desc">查看营业数据</div>
              </div>
              <div class="action-card">
                <div class="action-icon">🍽️</div>
                <div class="action-title">商品管理</div>
                <div class="action-desc">管理菜品信息</div>
              </div>
              <div class="action-card">
                <div class="action-icon">👥</div>
                <div class="action-title">会员管理</div>
                <div class="action-desc">管理会员信息</div>
              </div>
              <div class="action-card">
                <div class="action-icon">🛒</div>
                <div class="action-title">前台点单</div>
                <div class="action-desc">处理订单</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { logout } from '../api/auth'
import { useMessage } from '../composables/useMessage'

const router = useRouter()
const { success, error } = useMessage()

const userInfo = ref({})

const userTypeText = computed(() => {
  const typeMap = {
    'superadmin': '超级管理员',
    'admin': '管理员',
    'user': '普通用户'
  }
  return typeMap[userInfo.value.userType] || '未知'
})

const handleLogout = async () => {
  try {
    await logout()
    success('退出登录成功')
    setTimeout(() => {
      router.push('/login')
    }, 1000)
  } catch (e) {
    error('退出登录失败：' + e.message)
  }
}

onMounted(() => {
  // 从本地存储获取用户信息
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    userInfo.value = JSON.parse(storedUserInfo)
  }
})
</script>

<style scoped>
.dashboard {
  min-height: 100vh;
  background: #f5f5f5;
}

.dashboard-header {
  background: white;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 0 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
}

.header-left h1 {
  margin: 0;
  color: #1e69de;
  font-size: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.welcome {
  color: #333;
  font-weight: 500;
}

.user-type {
  background: #e6f7ff;
  color: #1890ff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.logout-btn {
  background: #ff4d4f;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}

.logout-btn:hover {
  background: #ff7875;
}

.dashboard-main {
  padding: 24px;
}

.welcome-card {
  background: white;
  border-radius: 12px;
  padding: 32px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-card h2 {
  color: #1e69de;
  margin-bottom: 8px;
}

.welcome-card > p {
  color: #666;
  margin-bottom: 32px;
}

.user-details {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 32px;
}

.detail-item {
  display: flex;
  margin-bottom: 12px;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.detail-item label {
  width: 100px;
  color: #666;
  font-weight: 500;
}

.detail-item span {
  color: #333;
}

.quick-actions h3 {
  color: #333;
  margin-bottom: 20px;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.action-card {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  border: 2px solid transparent;
}

.action-card:hover {
  background: #e6f7ff;
  border-color: #1890ff;
  transform: translateY(-2px);
}

.action-icon {
  font-size: 32px;
  margin-bottom: 12px;
}

.action-title {
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.action-desc {
  color: #666;
  font-size: 14px;
}

@media (max-width: 768px) {
  .dashboard-header {
    padding: 0 16px;
    flex-direction: column;
    height: auto;
    padding: 16px;
  }
  
  .header-left {
    margin-bottom: 12px;
  }
  
  .user-info {
    flex-direction: column;
    gap: 8px;
  }
  
  .dashboard-main {
    padding: 16px;
  }
  
  .welcome-card {
    padding: 20px;
  }
  
  .action-grid {
    grid-template-columns: 1fr;
  }
}
</style>
